package com.cj.mobile.iabp.mvp.model;

import com.jess.arms.cj.ApiOperator;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;
import com.cj.mobile.iabp.mvp.contract.ForgotPasswordContract;
import com.cj.mobile.iabp.mvp.model.api.service.AccountService;
import com.jess.arms.cj.CommonResponse;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;


@ActivityScope
public class ForgotPasswordModel extends BaseModel implements ForgotPasswordContract.Model {
    @Inject
    ApiOperator apiOperator;                                                                        // 数据转换

    @Inject
    public ForgotPasswordModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.apiOperator = null;
    }

    @Override
    public Observable<CommonResponse> forgotPassword(String account, String code, String newPwd) {
        Map<String, Object> params = new HashMap<>();
        params.put("userPhone", account);
        params.put("code", code);
        params.put("newPwd", newPwd);
        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).forgotPassword(request));
    }

    @Override
    public Observable<CommonResponse> sendSms(String userPhone) {
        Map<String, Object> params = new HashMap<>();
        params.put("userPhone", userPhone);
        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).sendSms(request));
    }
}